package edu.ihs.lhpy.install;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import edu.ihs.lhpy.domain.BaseInfo;
import edu.ihs.lhpy.domain.Privilege;
import edu.ihs.lhpy.domain.User;
@Component
public class Installer {

	@Resource
	private SessionFactory sessionFactory;
	
	@Transactional
	public void Install(){
		Session session = sessionFactory.getCurrentSession();
		//超级管理员
		User user = new User();
		user.setRealName("超级管理员");
		user.setName("admin");
		user.setPassword(DigestUtils.md5Hex("admin123"));
		session.save(user);
		
		//给baseinfo表格插入type类型
		Date tody = new Date();
		BaseInfo baseInfo;
		baseInfo = new BaseInfo("简介",tody,"admin","请填写内容","joinIntroduce");//插入联合培养简介类别
		session.save(baseInfo);
		baseInfo = new BaseInfo("培养方案",tody,"admin","请填写内容","trainPlan");//培养方案
		session.save(baseInfo);
		baseInfo = new BaseInfo("学位授予",tody,"admin","请填写内容","degreeGrant");//学位授予
		session.save(baseInfo);
		baseInfo = new BaseInfo("工作流程",tody,"admin","请填写内容","workFlow");//工作流程
		session.save(baseInfo);
		baseInfo = new BaseInfo("联系方式",tody,"admin","请填写内容","contact");//联系方式
		session.save(baseInfo);
		baseInfo = new BaseInfo("奖助体系",tody,"admin","请填写内容","award");//奖助体系
		session.save(baseInfo);
		//权限的数据
		// 二、权限数据
		Privilege menu, menu1, menu2, menu3, menu4, menu5;

		menu = new Privilege("网站常规管理", null, "FUNC20082.gif", null);
		menu1 = new Privilege("欢迎界面", "adminAction_right", null, menu);
		menu2 = new Privilege("管理员管理", "userAction_list", null, menu);
		session.save(menu);
		session.save(menu1);
		session.save(menu2);

		// -------------------------

		menu = new Privilege("基本信息管理", null, "FUNC20064.gif", null);
		menu1 = new Privilege("联合培养简介", "baseInfoAction_joinIntroduce", null, menu);
		menu2 = new Privilege("联合机构", "instituteAction_list", null, menu);

		session.save(menu);
		session.save(menu1);
		session.save(menu2);

		// -------------------------

		menu = new Privilege("最新动态", null, "FUNC20064.gif", null);
		menu1 = new Privilege("最新动态", "newsAction_list", null, menu);
		session.save(menu);
		session.save(menu1);
		// -------------------------

		menu = new Privilege("导师简介", null, "FUNC20064.gif", null);
		menu1 = new Privilege("导师信息", "teacherAction_list", null, menu);

		session.save(menu);
		session.save(menu1);
		// -------------------------

		menu = new Privilege("培养工作", null, "FUNC20064.gif", null);
		menu1 = new Privilege("培养方案", "baseInfoAction_trainPlan", null, menu);
		menu2 = new Privilege("学位授予", "baseInfoAction_degreeGrant", null, menu);
		menu3 = new Privilege("工作流程", "baseInfoAction_workFlow", null, menu);

		session.save(menu);
		session.save(menu1);
		session.save(menu2);
		session.save(menu3);
		
		// -------------------------

		menu = new Privilege("招生工作", null, "FUNC20064.gif", null);
		menu1 = new Privilege("招生简章", "studentsGuideAction_list", null, menu);
		menu2 = new Privilege("联系方式", "baseInfoAction_contact", null, menu);

		session.save(menu);
		session.save(menu1);
		session.save(menu2);
		
		// -------------------------

		menu = new Privilege("奖助体系", null, "FUNC20064.gif", null);
		menu1 = new Privilege("奖助体系", "baseInfoAction_award", null, menu);

		session.save(menu);
		session.save(menu1);
		// -------------------------

		menu = new Privilege("办事指南", null, "FUNC20064.gif", null);
		menu1 = new Privilege("办事指南", "workGuideAction_list", null, menu);

		session.save(menu);
		session.save(menu1);
		
		
	}
	public static void main(String args[]){
		System.out.println("正在安装......");
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Installer installer = (Installer) ac.getBean("installer");
		installer.Install();
		System.out.println("==安装完毕！==");
	}
}
